﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMLTouch.DomainRecognition;
using UMLTouch.DomainRecognition.Engines;
using UMLTouch.Domain.ClassDiagram.Items;
using System.Windows;
using System.Windows.Controls;
using UMLTouch.Core.Entities;
using System.Windows.Threading;
using UMLTouch.DomainRecognition.ShapeRecognition;
using UMLTouch.DomainRecognition.SegmentationAlgorithms;
using UMLTouch.Domain.ClassDiagram.Patterns;
using UMLTouch.Core.UI;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace UMLTouch.Domain.ClassDiagram
{

    public class ClassDiagramContext : IDiagramContext
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        ClassDiagramItemsCollection _innerCollection;

        protected ClassDiagramItemsCollection InnerCollection
        {
            get { return _innerCollection; }
            private set
            {
                _innerCollection = value;
                _innerCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(_innerCollection_CollectionChanged);
            }
        }

        void _innerCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RaiseCollectionChanged(sender, e);
        }

        public string Name
        {
            get;
            set;
        }
        public string Path
        {
            get;
            set;
        }

        RecognitionEngine _engine;

        public DiagramItemsCollection DiagramItems
        {
            get
            {
                return InnerCollection;
            }
        }

        public ClassDiagramContext(String name)
        {
            InnerCollection = new ClassDiagramItemsCollection();
            ShapeRecognizer shapeRecognizer = new ClassDiagramRecognizer();
            ISegmentationAlgorithm segmentationAlgorithm = new PersonnalAlgorithm();
            _engine = new RecognitionEngine(shapeRecognizer, segmentationAlgorithm);

            Name = name;

            BindEngine();
        }

        private void BindEngine()
        {
            _engine.OnRecognizedItem += new RecognitionEngine.RecognizedItem(_engine_OnRecognizedItem);
        }

        void _engine_OnRecognizedItem(IDiagramItem recognizedItem)
        {


            if (recognizedItem != null && !(recognizedItem is UnknownItem))
            {
                AddItem(recognizedItem);
            }
        }

        public void AddItem(IDiagramItem recognizedItem)
        {
            DiagramItems.Add(recognizedItem);
        }

        public void InputStroke(Stroke stroke)
        {
            _engine.InputItem(stroke);
        }

        void RaiseCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(sender, e);
            }
        }
    }
}
